package com.jpsl.core.reflection.wrapper;

import com.jpsl.core.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * map 包装
 *
 * @author 懒王猫
 */
public class MapWrapper implements ObjectWrapper {

    private final Map<String, Object> map;

    public MapWrapper(Map<String, Object> map) {
        Assert.notNull(map, "map");
        this.map = map;
    }

    @Override
    public List<String> getProperties() {
        return new ArrayList<>(map.keySet());
    }

    @Override
    public boolean hasGetter(String property) {
        return map.containsKey(property);
    }

    @Override
    public boolean hasSetter(String property) {
        return true;
    }

    @Override
    public Object getValue(String property) {
        return map.get(property);
    }

    @Override
    public Class<?> getGetterType(String property) {
        Object value = getValue(property);
        return value == null ? null : value.getClass();
    }

    @Override
    public void setValue(String property, Object value) {
        map.put(property, value);
    }

    @Override
    public Class<?> getSetterType(String property) {
        return null;
    }
}
